package com.augurit.common.system.web.org;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.system.service.org.IAcModuleService;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.org.form.AcModuleForm;
import com.augurit.common.util.ExtUtils;

@SuppressWarnings("serial")
@Results({
	@Result(name = ExtCrudActionSupport.RELOAD, location = "ac-module.action", type = "redirect"),
	@Result(name = ExtCrudActionSupport.SUCCESS, location = "/common/system/org/ac-module.jsp")
})
public class AcModuleAction extends ExtCrudActionSupport<AcModuleForm> {

	@Autowired
	private IAcModuleService moduleService;
	
	// 属性
	private AcModuleForm form;
	private Long id;
	private Long[] ids;
	private Page<AcModuleForm> page = new Page<AcModuleForm>(15);
	private Long appId;

	/**
	 * 分页获取指定应用的所有模块及子模块
	 */
	@Override
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		moduleService.getAllModules(id, page);
		this.extRenderGridJson(page);
		return null;
	}	

	/**
	 * 新增或编辑模块页面
	 */
	@Override
	public String input() throws Exception {
		this.extRenderFormJson(form);
		return null;
	}
	
	/**
	 * 保存模块信息
	 */
	@Override
	public String save() throws Exception {
		moduleService.addModuleCascade(form);
		this.extRenderSuccess();
		return null;
	}

	/**
	 * 获取应用的直属模块列表
	 */
	public String getModules() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		moduleService.getModules(appId, page, WebContextUtils.getSysCodeBuffer(this.getApplication()));
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 获取父模块下的直属模块列表
	 */
	public String getChildModules() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		moduleService.getChildModules(id, page, WebContextUtils.getSysCodeBuffer(this.getApplication()));
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 删除一个或多个模块
	 */
	@Override
	public String delete() throws Exception {
		moduleService.deleteModuleCascade(ids);
		this.extRenderSuccess();
		return null;
	}
	
	
	
	public AcModuleForm getModel() {
		return form;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			form = moduleService.get(id);
		}else{
			form = new AcModuleForm();
		}
	}
	
	
	
	// 属性getter和setter方法
	public AcModuleForm getForm() {
		return form;
	}

	public void setForm(AcModuleForm form) {
		this.form = form;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public Long getAppId() {
		return appId;
	}

	public void setAppId(Long appId) {
		this.appId = appId;
	}
}
